<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="createtask">
        新建任务
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      style="width:100%;margin-top: 15px;"
    >
      <el-table-column align="center" prop="taskId" label="任务ID" min-width="8%" />
      <el-table-column align="center" prop="storeId" label="数据源ID" min-width="8%" />
      <el-table-column align="center" prop="targetId" label="目标服务ID" min-width="8%" />
      <el-table-column label="所在节点名称" min-width="5%" align="center" prop="nodeName" />
      <el-table-column label="运行状态" min-width="5%" align="center" prop="status" />
      <el-table-column label="启动时间" min-width="5%" align="center" prop="createdAt" />
      <el-table-column label="更新时间" min-width="5%" align="center" prop="updatedAt" />
      <el-table-column label="进度" min-width="5%" align="center" prop="percent" />
      <el-table-column label="操作" min-width="10%" align="center">
        <template #default="scope">
          <el-button size="mini" @click="startTask(scope.$index, scope.row)">开始</el-button>
          <el-button size="mini" @click="stopTask(scope.$index, scope.row)">暂停</el-button>
          <el-button size="mini" type="danger" @click="deleteTask(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getTaskList" />
    <el-dialog v-loading="listLoading" :title="title" :visible.sync="dialogFormVisible" width="30%">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="选择节点" prop="type">
          <el-select v-model="temp.nodeid" class="filter-item" placeholder="请选择节点" @change="selectnodechange">
            <el-option v-for="item in nodeoptionlist" :key="item.nodeid" :label="item.nodename" :value="item.nodeid" />
          </el-select>
        </el-form-item>
        <el-form-item label="数据源服务ID" prop="type">
          <el-select v-model="temp.storeid" class="filter-item" placeholder="请选择服务" @change="selectstorechange">
            <el-option v-for="item in storelist" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="目标服务ID" prop="type">
          <el-select v-model="temp.targetid" class="filter-item" placeholder="请选择服务" @change="selecttargetchange">
            <el-option v-for="item in storelist" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="handlerCreateNewTask">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { syncTaskList } from '@/api/sync'
import { createfullTask } from '@/api/sync'
import { startFullTask } from '@/api/sync'
import { stopFullTask } from '@/api/sync'
import { removeFullTask } from '@/api/sync'
import { getnodeidnamelist } from '@/api/node'
import { findstoreids } from '@/api/store'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  data() {
    return {
      timer: '',
      list: null,
      listLoading: false,
      dialogFormVisible: false,
      total: 0,
      title: '迁移数据',
      listQuery: {
        pageNum: 1,
        pageSize: 10
      },
      nodeoptionlist: [],
      storelist: [],
      temp: {
        nodeid: '',
        storeid: '',
        targetid: ''
      }
    }
  },
  // mounted() {
  //   this.timer = setInterval(() => { this.getTaskList() }, 3000)
  // },
  // beforeDestroy() {
  //   clearInterval(this.timer)
  // },
  created() {
    this.getTaskList()
  },
  methods: {
    getTaskList() {
      this.listLoading = true
      syncTaskList({ pageNum: this.listQuery.pageNum, pageSize: this.listQuery.pageSize }).then(response => {
        this.total = response.data.total
        this.list = response.data.list
        this.listLoading = false
      })
    },
    Nodelist() {
      getnodeidnamelist().then(response => {
        this.nodeoptionlist = response.data
      })
    },
    selectnodechange(current) {
      this.temp.nodeid = current
      this.findstoreidsByNodeId()
    },
    selectstorechange(current) {
      this.temp.storeid = current
    },
    selecttargetchange(current) {
      this.temp.targetid = current
    },
    findstoreidsByNodeId() {
      findstoreids({ nodeId: this.temp.nodeid }).then(response => {
        this.storelist = response.data.storeIds
      })
    },
    createtask() {
      this.resetTemp()
      this.dialogFormVisible = true
      this.Nodelist()
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handlerCreateNewTask() {
      if (this.temp.targetid === this.temp.storeid) {
        this.$message({
          message: '源服务Id和目标服务Id不能一致',
          type: 'error',
          duration: 5 * 1000
        })
        return
      }
      createfullTask({ targetId: this.temp.targetid, storeId: this.temp.storeid }).then(response => {
        if (response.code === '0') {
          this.showsucess(response.msg)
          this.getTaskList()
        }
        this.dialogFormVisible = false
      })
    },
    showsucess(msg) {
      this.$message({
        message: msg,
        type: 'success',
        duration: 5 * 1000
      })
    },
    resetTemp() {
      this.storelist = []
      this.temp = {
        nodeid: '',
        storeid: '',
        targetid: ''
      }
    },
    startTask(index, row) {
      console.log(row.taskId)
      startFullTask({ taskId: row.taskId }).then(response => {
        if (response.code === '0') {
          this.showsucess(response.msg)
          this.getTaskList()
        }
      })
    },
    stopTask(index, row) {
      stopFullTask({ taskId: row.taskId }).then(response => {
        if (response.code === '0') {
          this.showsucess(response.msg)
          this.getTaskList()
        }
      })
    },
    deleteTask(index, row) {
      removeFullTask({ taskId: row.taskId }).then(response => {
        if (response.code === '0') {
          this.showsucess(response.msg)
          this.getTaskList()
        }
      })
    }
  }
}
</script>

<style>

</style>
